<p:config xmlns:p="http://www.orbeon.com/oxf/pipeline" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:oxf="http://www.orbeon.com/oxf/processors" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" xmlns:xdb="http://orbeon.org/oxf/xml/xmldb" xmlns:xu="http://www.xmldb.org/xupdate">

	<p:param type="input" name="instance"/>

	<p:processor name="oxf:pipeline">
		<p:input name="config" href="../session.xpl"/>
		<p:output name="data" id="session"/>
	</p:processor>
	<p:processor name="oxf:xslt">
		<p:input name="data" href="#instance"/>
		<p:input name="session" href="#session"/>
		<p:output name="data" id="query"/>
		<p:input name="config">
			<xdb:query collection="/db/portal" xsl:version="2.0">
				<xsl:text>xquery version "1.0";</xsl:text>
				let $max := number(substring-after(/groups/group[position() = last()]/@id, '_')) + 1 return
				if ('<xsl:value-of select="string(/list/new/name)"/>' != '' and not(exists(/groups/group/name[text() = '<xsl:value-of select="string(/list/new/name)"/>']))) then
				(update insert
				element group{
					attribute id{concat('gr_', $max)},
					element name{'<xsl:value-of select="string(replace(/list/new/name, '''', ''''''))"/>'},
					element description{'<xsl:value-of select="string(replace(/list/new/description,'''',''''''))"/>'},
					element members{
						element member{
							attribute id{'<xsl:value-of select="doc('input:session')/session/username"/>'},
							attribute level{'2'}
						}
					},
					element interface{}
				} into /groups,
				<ok/>) else <error/>
			</xdb:query>
		</p:input>
	</p:processor>

	<p:processor name="oxf:xmldb-query">
		<p:input name="datasource" href="/config/datasource.xml"/>
		<p:input name="query" href="#query"/>
		<p:output name="data" id="result"/>
	</p:processor>
	<p:choose href="#result">
		<p:when test="exists(/error)">
			<p:processor name="oxf:pipeline">
				<p:input name="config" href="univers-new.xpl"/>
			</p:processor>
		</p:when>
		<p:otherwise>
			<p:processor name="oxf:redirect">
				<p:input name="data">
					<redirect-url>
						<path-info>/portal/main/</path-info>
					</redirect-url>
				</p:input>
			</p:processor>
		</p:otherwise>
	</p:choose>


</p:config>